﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Web;
using System.Web.Mvc;
using System.Web.Services.Description;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{

    public class DefaultController : Controller
    {
        private SchoolEntities _context;
        public DefaultController()
        {
            _context = new SchoolEntities();
        }
        // GET: Default
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {

            var course = _context.Course.ToList();
            return View(course);
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Detail(int id)
        {
            var course = _context.Course.Find(id);
            return View(course);
        }


        /// <summary>
        /// 获取用户id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Modify(int id)
        {
            var course = _context.Course.Find(id);
            return View(course);
        }

        /// <summary>
        /// 修改学生资料
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Modify()
        {
            var id = Convert.ToInt32(Request["CourseID"]);
            var title = Request["Title"];
            var credits = Convert.ToInt32(Request["Credits"]);

            var course = _context.Course.Find(id);
            course.Title = title;
            course.Credits = credits;
            _context.SaveChanges();

            return RedirectToAction("index");
        }

        /// <summary>
        /// 删除学生
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Remove(int id)
        {
            var course = _context.Course.Find(id);
            _context.Course.Remove(course);
            _context.SaveChanges();
            return RedirectToAction("index");
        }

        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(string title, int credits)
        {

            var table = new Course();

            table.Title = Request["title"];
            table.Credits = Convert.ToInt32(Request["credits"]);

            _context.Course.Add(table);
            _context.SaveChanges();
            return RedirectToAction("index");


        }

    }
}